package com.ss.android.video.b;

import android.util.Pair;

import com.bytedance.common.utility.b.ThreadPlus;
import com.bytedance.retrofit2.ResponseWrapper;
import com.bytedance.retrofit2.client.Header;
import com.bytedance.ttnet.INetworkApi;
import com.bytedance.ttnet.utils.RetrofitUtils;
import com.ss.ttvideoengine.c.HttpRequest;
import com.ss.ttvideoengine.d.TTVideoError;

import org.json.JSONObject;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.LinkedList;

import inline.NetworkDemo;

final class MediaPlayerNetClient extends ThreadPlus {
    private final MediaPlayerHttpRequest c;
    private final String mUrl;
    private final HttpRequest.Callback mCallback;

    MediaPlayerNetClient(MediaPlayerHttpRequest arg1, String name, String url, HttpRequest.Callback callback) {
        super(name);
        this.c = arg1;
        this.mUrl = url;
        this.mCallback = callback;
    }

    public final void run() {
        LinkedHashMap<String, String> queryMap = new LinkedHashMap();
        Exception exception;
        try {
            Pair<String, String> pair = NetworkDemo.parseUrl(this.mUrl, queryMap);
            String website = pair.first;
            String path = pair.second;
            INetworkApi networkApi = RetrofitUtils.createService(website, INetworkApi.class);
            LinkedList<Header> headers = new LinkedList();
            if (networkApi == null) {
                return;
            }
            this.c.mCall = networkApi.doGet(true, 20480, path, queryMap, headers, null);
            ResponseWrapper<String> responseWrapper = this.c.mCall.execute();
            JSONObject respJo;
            int code;
            if (responseWrapper == null) {
                exception = new Exception("null response");
                respJo = null;
                code = -1;
            } else {
                String body = responseWrapper.body;
                if (!responseWrapper.rawResponse.success()) {
                    exception = new Exception("http fail");
                    code = responseWrapper.rawResponse.status;
                    respJo = null;
                } else {
                    try {
                        respJo = new JSONObject(body);
                        exception = null;
                        code = -1;
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        code = -9994;
                        respJo = null;
                        exception = ex;
                    }
                }
            }

            if (exception != null) {
                if (this.mCallback != null) {
                    this.mCallback.callback(null, new TTVideoError("", code, exception.toString()));
                    return;
                } else {
                    return;
                }
            }
            if (this.mCallback != null) {
                if (respJo != null) {
                    this.mCallback.callback(respJo, null);
                    return;
                } else {
                    this.mCallback.callback(null, new TTVideoError("", code, "null json"));
                    return;
                }
            }
        } catch (IOException ioException) {
            if (this.mCallback != null) {
                this.mCallback.callback(null, new TTVideoError("", -1, ioException.toString()));
            }
        }
    }
}

